﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcJqGrid;
using PagedList;
using WebApp.Services;
using System.Globalization;
using WebApp.Commons;
using WebApp.Data;

namespace WebApp.Controllers
{
    public class TheProjectController : Controller
    {
        //
        // GET: /TheProject/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult AreaDesign() 
        {
            return View();
        }

        private TheProjectService _service;

        public JsonResult GetProjectByCustomerId(GridSettings gridSettings, string custId)
        {
            _service = new TheProjectService();

            var realCount = 0;

            var datas = _service.GetProjectListByCustId(gridSettings.PageIndex,
                                          gridSettings.PageSize,
                                          gridSettings.SortColumn,
                                          gridSettings.SortOrder,
                                          custId,
                                          ref realCount);

            var totalBUs = realCount;
            var totalPage = totalBUs / gridSettings.PageSize;

            if (totalPage > Convert.ToInt32(totalPage))
            {
                totalPage = totalPage + 1;
            }
            var jsonData = new
            {
                total = Convert.ToInt32(totalPage),
                page = gridSettings.PageIndex,
                records = totalBUs,
                rows = (
                            from c in datas
                            select new
                            {
                                id = c.project_id,
                                cell = new[] {
                                            c.project_id.ToString(CultureInfo.InvariantCulture),
                                            c.project_location,
                                            _service.GetCustName(c.customer_id).customer_firstname,
                                            c.project_update_date.HasValue ? c.project_update_date.Value.ToLongDateString() : ""
                                            }
                            }
                        ).ToPagedList(1, gridSettings.PageSize).ToArray()
            };

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public JsonResult List(GridSettings gridSettings, string firstName, string tel, string address, string date)
        {
            _service = new TheProjectService();

            var realCount = 0;

            var datas = _service.GetAreaType(gridSettings.PageIndex,
                                          gridSettings.PageSize,
                                          gridSettings.SortColumn,
                                          gridSettings.SortOrder,
                                          firstName, tel, address, date,
                                          ref realCount);

            var totalBUs = realCount;
            var totalPage = totalBUs / gridSettings.PageSize;

            if (totalPage > Convert.ToInt32(totalPage))
            {
                totalPage = totalPage + 1;
            }
            var jsonData = new
            {
                total = Convert.ToInt32(totalPage),
                page = gridSettings.PageIndex,
                records = totalBUs,
                rows = (
                            from c in datas
                            select new
                            {
                                id = c.project_id,
                                cell = new[] {
                                            c.project_id.ToString(CultureInfo.InvariantCulture),
                                            c.project_location,
                                            _service.GetCustName(c.customer_id).customer_firstname,
                                            c.project_create_date.HasValue ? c.project_create_date.Value.ToLongDateString() : ""
                                            }
                            }
                        ).ToPagedList(1, gridSettings.PageSize).ToArray()
            };

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public JsonResult CreateOrUpdateProject(tbl_project oModel, FormCollection formCollection)
        {
            _service = new TheProjectService();

            string projectId = "";
            string isPass = "true";
            string iMessage = string.Empty;
            
            string[] output = _service.CreateOrUpdateProject(oModel);

            projectId = output[0];
            isPass = output[1];
            iMessage = output[2];
            return Json(new { projectId, isPass, iMessage });
        }

        public JsonResult Delete(int[] ids, FormCollection formCollection)
        {
            _service = new TheProjectService();

            var isPass = false;
            var iMessage = string.Empty;

            var operation = formCollection["oper"];

            if (operation.Equals("del"))
            {
                isPass = _service.Delete(ids);
            }

            return Json(new { isPass, iMessage });
        }

        public String GetCust()
        {
            var sTag = DataProvider.GetCustomer().Aggregate("<select>", (current, type) => current + "<option value='" + type.customer_id + "'>" + type.customer_firstname + " " + type.customer_lastname + "</option>");

            return sTag + "</select>";
        }

        public JsonResult GetProjectArea(GridSettings gridSettings, string project_id) 
        {
            _service = new TheProjectService();
            int count = 0;
            var datas = _service.GetProjectArea(gridSettings.PageIndex,
                                          gridSettings.PageSize,
                                          gridSettings.SortColumn,
                                          gridSettings.SortOrder, project_id, ref count);
            var totalBUs = count;
            var totalPage = totalBUs / gridSettings.PageSize;

            if (totalPage > Convert.ToInt32(totalPage))
            {
                totalPage = totalPage + 1;
            }
            var jsonData = new
            {
                total = Convert.ToInt32(totalPage),
                page = gridSettings.PageIndex,
                records = totalBUs,
                rows = (
                            from c in datas
                            select new
                            {
                                id = c.project_area_id,
                                cell = new[] {
                                            c.project_id.ToString(CultureInfo.InvariantCulture),
                                            c.project_area_id.ToString(CultureInfo.InvariantCulture),
                                            //_service.GetCustName(c.customer_id).customer_firstname
                                            c.area_type_id.ToString(),
                                            DataProvider.GetAreaTypeNameById(c.area_type_id),
                                            c.project_area_image,
                                            c.project_area_name,
                                            c.project_area_outline_url,
                                            c.project_area_size.ToString(),
                                            c.project_paving_type,
                                            c.project_area_create_date.HasValue? c.project_area_create_date.Value.ToLongDateString() : "",
                                            c.project_area_create_by.ToString(),
                                            c.project_area_update_date.HasValue? c.project_area_update_date.Value.ToLongDateString() : "",
                                            c.project_area_update_by.ToString(),
                                            c.is_active.ToString()
                                            }
                            }
                        ).ToPagedList(1, gridSettings.PageSize).ToArray()
            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public string GetAreaTypeListHtml()
        {
            _service = new TheProjectService();
            var sTag = _service.GetAreaType().Aggregate("<select>", (current, area) => current + "<option value='" + area.area_type_id + "'>" + area.area_type_name + "</option>");

            return sTag + "</select>";
        }

        public JsonResult UpdateProjectArea(tbl_project_area area, string oper)
        {
            _service = new TheProjectService();

            var isPass = false;
            var iMessage = string.Empty;
            if (string.IsNullOrEmpty(oper))
            {
                iMessage = "oper is null";
                return Json(new { isPass, iMessage });
            }
            var operation = oper;

            if (operation.Equals("add") || operation.Equals("edit"))
            {
                _service.UpdateProjectArea(area);
            }
            else if (operation.Equals("del"))
            {
                _service.DeleteProjectArea(area);
            }
            isPass = true;
            return Json(new { isPass, iMessage });
        }
    }
}
